package com.briup.smartcity.service.impl;

import cn.hutool.json.JSONUtil;
import com.briup.smartcity.bean.BaseConfig;
import com.briup.smartcity.bean.Carousel;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.BaseConfigMapper;
import com.briup.smartcity.mapper.extend.BaseConfigExtendMapper;
import com.briup.smartcity.service.IBaseConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.regexp.joni.Config;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseConfigServiceImpl implements IBaseConfigService {
    @Autowired
    private BaseConfigMapper mapper;
    @Autowired
    private BaseConfigExtendMapper extendMapper;

    @Override
    public List<BaseConfig> findYesBaseConfig() throws ServiceException {
        return extendMapper.findYesBaseConfig();
    }

    @Override
    public PageInfo<Config> findAllConfig(int pageNum, int pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Config> list = extendMapper.findAllConfig();
        return new PageInfo<>(list);
    }

    @Override
    public void saveConfig(BaseConfig baseConfig) throws ServiceException {
        baseConfig.setConfigStatus(0);
        String ConfigName = baseConfig.getConfigName();
        String ConfigIcon = baseConfig.getConfigIcon();
        String ConfigInfo = baseConfig.getConfigInfo();
        if( ConfigName == null){
            throw new ServiceException("请输入系统名称");
        }
        if( ConfigIcon == null){
            throw new ServiceException("请输入图标地址");
        }
        if( ConfigInfo == null){
            throw new ServiceException("请输入系统信息");
        }
        String configName = extendMapper.selectNamebyName(ConfigName);
        if(configName != null){
            throw new ServiceException("该系统名称已存在");
        }
        mapper.insertSelective(baseConfig);
    }

    @Override
    public void updateConfig(BaseConfig baseConfig) throws ServiceException {
        String ConfigName = baseConfig.getConfigName();
        String ConfigIcon = baseConfig.getConfigIcon();
        String ConfigInfo = baseConfig.getConfigInfo();
        if( ConfigName == null){
            throw new ServiceException("请输入系统名称");
        }
        if( ConfigIcon == null){
            throw new ServiceException("请输入图标地址");
        }
        if( ConfigInfo == null){
            throw new ServiceException("请输入系统信息");
        }
        extendMapper.updateConfigBySelect(baseConfig);
    }

    @Override
    public void deleteById(int id) {
        String Name = extendMapper.selectNameById(id);
        if(Name == null ){
            throw new ServiceException("该系统信息不存在");
        }
        mapper.deleteByPrimaryKey(id);
    }


    @Override
    public void updateConfigStatus(Integer id,Integer status) throws ServiceException {


        String s = extendMapper.selectNameByStatus(1);
        if (s != null) {
            int confid = extendMapper.selectConfidByStatus(1);
            if (id != confid) {
                throw new ServiceException("必须先禁用启用状态的配置");
            } else if (status == 1) {
                extendMapper.updateStatusById2(id);
            } else {
                extendMapper.updateStatusById(id);
            }
        } else {
            if (status == 1) {
                extendMapper.updateStatusById2(id);
            } else {
                extendMapper.updateStatusById(id);
            }
        }
    }
//    @Override
//    public void changeConfigStatus(int id, int status) throws ServiceException {
//        if(status == 1){
//            BaseConfig baseConfig = extendMapper.currentConfigYes();
//            if(baseConfig == null){
//                extendMapper.changeConfigStatus(id,status);
//            }else if(baseConfig.getConfigId() != id && baseConfig !=null){
//                throw new ServiceException("必须先禁用启用状态的系统配置");
//            }
//        }
//        if(status == 0){
//            extendMapper.changeConfigStatus(id,status);
//        }
//    }

}
